<template>
	<view>
		<view class="bar">
			<view class="bottom-bar">
				<view class="bt-img">
					<image src="../../static/input.png" :class="{displaynone:!isvoice}" @tap="isvoice=!isvoice"></image>
				</view>
				<view class="bt-img">
					<image src="../../static/voice.png" :class="{displaynone:isvoice}" @tap="isvoice=!isvoice"></image>
				</view>
				<textarea auto-height class="input-content btn" :class="{displaynone:isvoice}" @input="inputs" @focus="InputFocus" @blur="InputBlur" v-model="msg"></textarea>
				<view class="voice btn" :class="{displaynone:!isvoice}">按住说话</view>
				<view class="bt-img" @tap="emoji" >
					<image src="../../static/smile.png"></image>
				</view>
				<view class="bt-img" @tap="more">
					<image src="../../static/add-yellow.png"></image>
				</view>
			</view>
			<view class="emoji" :class="{displaynone:isemoji}">
				<view class="emoji-send">
					<view class="emoji-cancel-btn" @tap="emojiCancel">
						<image src="../../static/rback64.png"></image>
					</view>
					<view class="emoji-send-btn" @tap="emojiSend">发送</view>
				</view>
				<emoji @clickEmoji="clickEmoji" :height="260"></emoji>
			</view>
			
			<view class="more" :class="{displaynone:ismore}">
				更多
			</view>
		</view>
	</view>
</template>

<script>
	import emoji from './emoji/emoji.vue'
	export default {
		components:{
			emoji
		},
		name: "imbottombar",
		data() {
			return {
				isvoice: false,
				isemoji: true,
				ismore: false,
				msg: ''
			};
		},
		methods:{
			// 取消表情
			emojiCancel:function(e){
				if(this.msg.length > 0){
					this.msg = this.msg.substring(0,this.msg.length-2)
				}
			},
			// 发送表情
			emojiSend:function(e){
				if(this.msg.length > 0){
					// 表情也是文字，所以type为0
					this.send(this.msg,0)
					// this.$emit('inputs',this.msg)
					// setTimeout(()=>{
					// 	this.msg = '';
					// },0)
				}
			},
			InputFocus:function(){
				// 聚焦不处理，手机系统自动处理
				// 关闭emojie
				// this.isemoji = true
				// // 获取emoji高度
				// setTimeout(()=>{
				// 	this.getEleHeight();
				// },10)
			},
			InputBlur:function(){
				// // 关闭emojie
				// this.isemoji = true
				// // 获取emoji高度
				// setTimeout(()=>{
				// 	this.getEleHeight();
				// },0)
			},
			// 发送消息
			send:function(msg,type){
				let data = {
					message: msg,
					type: type
				}
				this.$emit('inputs',data)
				setTimeout(()=>{
					this.msg = '';
				},0)
			},
			// 点击表情 接收表情
			clickEmoji:function(e){
				this.msg = this.msg + e
			},
			// 文字发送
			inputs:function(e){
				//  TODO 巨坑 e.detail.value只能取一次,再次获取失效
				var chatm = e.detail.value
				if(chatm.length > 0){
					this.moreOrSend = false
				}
				var pos = chatm.indexOf('\n')
				// 回车,提交
				if(pos != -1 && chatm.length > 0){
					// this.$emit('inputs',this.msg)
					this.send(this.msg,0)
					setTimeout(()=>{
						this.msg = '';
					},0)
				}
			},
			// 表情
			emoji:function(){
				this.isemoji = !this.isemoji
				// 获取emoji高度
				setTimeout(()=>{
					this.getEleHeight();
				},20)
			},
			more:function(){
				console.log('more')
			},
			// 获取高度
			getEleHeight:function(){
				const query = uni.createSelectorQuery().in(this);
				query.select('.bar').boundingClientRect(data => {
					this.$emit('heights',data.height)
				}).exec();
			}
		}
	}
</script>

<style lang="scss">
	.emoji{
		width: 100%;
		height: 460rpx;
		background: rgba(236,237,238,1);
		box-shadow: rgba(0,0,0,0.1);
	}
	
	.displaynone {
		display: none;
	}
	
	.bar {
		background: rgba(244, 244, 244, 0.96);
		border-top: 1px solid $uni-border-color;
		width: 100%;
		position: fixed;
		bottom: 0;
		// z-index: 100;
		// 安全距离
		// padding-bottom: var(--status-bar-height);
		// 底部安全距离
		padding-bottom: env(safe-area-inset-bottom);
	}

	.bottom-bar {
		width: 100%;
		display: flex;
		align-items: flex-end;
		// 不往外扩展
		box-sizing: border-box;
		padding: 10rpx 12rpx;
		image {
			width: 56rpx;
			height: 56rpx;
			margin: 0 10rpx;
			flex: auto;
		}

		.btn {
			// 自动撑开宽度
			flex: auto;
			background-color: #FFF;
			border-radius: 16rpx;
			padding: 20rpx;
			max-height: 160rpx;
			margin: 0 10rpx;
		}
			
		.voice {
			text-align: center;
			// 不能给size否则会上下高度不兼容
			// font-size: 32rpx;
			font-weight: 400;
		}
	}
	
	// 不能放在 bottom-bar 内部，放内部不出效果
	.emoji{
		width: 100%;
		height: 460rpx;
		background-color: rgba(236,237,238,1);
		box-shadow: 0px -1rpx 0px 0px rgba(0,0,0,0.1);
		.emoji-send{
			width: 280rpx;
			height: 104rpx;
			padding-top: 24rpx;
			background-color: rgba(236,237,238,1);
			position: fixed;
			bottom: env(safe-area-inset-bottom);
			right: 0;
			display: flex;
			.emoji-send-btn{
				flex: 1;
				margin: 0 32rpx 0 20rpx;
				height: 80rpx;
				background: rgba(255,228,49,1);
				font-size: 32rpx;
				text-align: center;
				line-height: 80rpx;
				border-radius: 12px;
			}
			
			.emoji-cancel-btn{
				flex: 1;
				margin-left: 24rpx;
				height: 80rpx;
				// background: #FFFFFF;
				font-size: 32rpx;
				text-align: center;
				line-height: 80rpx;
				border-radius: 12px;
				image{
					width: 42px;
					height: 42px;
					bottom: 3px;
				}
			}
		}
	}
</style>
